﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmptoolWebApp.Models
{
    public class CacheModel
    {
        // ZX: There are primarily 2 main caching classes
        // 1. System.Web.Caching.Cache
        // 2. System.Runtime.Caching.MemoryCache
        // Microsoft recommend use of the System.Runtime.Caching.MemoryCache class.
        // See the below article:
        // https://msdn.microsoft.com/en-us/library/dd997357.aspx

        private static System.Runtime.Caching.MemoryCache globalCache = null;
        public static System.Runtime.Caching.MemoryCache GlobalCache
        {
            get
            {
                return globalCache;
            }
            set
            {
                globalCache = value;
            }
        }

        public enum CacheItemPolicyType
        {
            Test = 0, // For testing ONLY
            Default = 1
        }

        static void TestCacheItemRemovedCallback(System.Runtime.Caching.CacheEntryRemovedArguments args)
        {
            
        }

        static System.Runtime.Caching.CacheItemPolicy GetCacheItemPolicy(CacheItemPolicyType policyType)
        {
            System.Runtime.Caching.CacheItemPolicy policy = new System.Runtime.Caching.CacheItemPolicy();

            switch (policyType)
            {
                case CacheItemPolicyType.Test:
                    policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now, new TimeSpan(0, 0, 15));
                    policy.SlidingExpiration = System.Runtime.Caching.MemoryCache.NoSlidingExpiration;
                    policy.RemovedCallback = TestCacheItemRemovedCallback;
                    policy.Priority = System.Runtime.Caching.CacheItemPriority.Default;
                    break;
                case CacheItemPolicyType.Default:
                    policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now, new TimeSpan(0, 0, 15));
                    policy.SlidingExpiration = System.Runtime.Caching.MemoryCache.NoSlidingExpiration;
                    policy.Priority = System.Runtime.Caching.CacheItemPriority.Default;
                    break;
                default:
                    break;
            }

            return policy;
        }

        static CacheModel()
        {
            globalCache = System.Runtime.Caching.MemoryCache.Default;
        }

        public static void Add<T>(string key, T item)
        {
            System.Runtime.Caching.CacheItem cacheItem = new System.Runtime.Caching.CacheItem(key, item);

            GlobalCache.Set(cacheItem, GetCacheItemPolicy(CacheItemPolicyType.Test));
        }

        public static T Get<T>(string key) where T : class
        {
            T result = default(T);

            System.Runtime.Caching.CacheItem cacheItem = GlobalCache.GetCacheItem(key);

            if (cacheItem != null)
            {
                result = cacheItem.Value as T;
            }

            return result;
        }

    }
}